$.extend(true, $.fn.dataTable.defaults, {
    sPaginationType:"full_numbers",
    bPaginite:true,
    bInfo:false,
    searching:false,
    bLengthChange:false,
    autoWidth:false,
    bSort:false,
    processing:false,
    serverSide:false,
    bPaginate:false,
    createdRow:function(row, data, index) {
        $(row).addClass("text-c");
    },
    aoColumnDefs:[ {
        sClass:"center"
    } ],
    oLanguage:{
        bAutoWidth:true,
        sZeroRecords:"Nothing found - 没有记录",
        sInfo:"_START_ 到 _END_ 条,共 _TOTAL_ 条",
        sInfoEmpty:"显示0条记录",
        sProcessing:"<div style=''><img src='../static/img/loader.gif'><span>加载中...</span></div>",
        oPaginate:{
            sFirst:"首页",
            sPrevious:"",
            sNext:"",
            sLast:"末页"
        }
    }
});






//跳转添加页面type:1.全屏2.自适应
function toAdd(title,forwardUrl,type){
	if(seesionIsExist("admin") == "" || seesionIsExist("admin") == null) {
		forwardLogin();
		return false;
	}
	
	if(type === '1'){
		width = $(document).width()*0.5;
		hieght = $(document).height()*0.8;
		layer_show(title,forwardUrl,width,hieght);
	}else{
		var index = layer.open({
			type: 2,
			title: title,
			content: forwardUrl
		});
		layer.full(index);
	}
}

//跳转添加页面type:1.全屏2.自适应
function toEdit(title,forwardUrl,type){
	
	if(seesionIsExist("admin") == "" || seesionIsExist("admin") == null) {
		forwardLogin();
		return false;
	}
	
	
	
	if(type === '1'){
		width = $(document).width()*0.6;
		hieght = $(document).height()*0.8;
		layer_show(title,forwardUrl,width,hieght);
	}else{
		var index = layer.open({
			type: 2,
			title: title,
			content: forwardUrl
		});
		layer.full(index);
	}
}

/**
 * 定义表格对象
 */
function Table() {
	/**
	 * 设置基本信息
	 */
	this.setting = function(pageUrl,delUrl,columns){
		this.pageUrl = pageUrl;
		this.delUrl = delUrl;
		this.columns = columns;
		
	};
	
	/**
	 * 分页方式
	 */
	this.tablePage = function(params){
		var $this = this;
		//设置默认初始页
		if(params ==undefined){
			params ={};
		}
		
		if(params.curPage == undefined){
			params.curPage = 1;
		}
		this.params = params;
		
		
		axios.get(this.pageUrl,{params:params})
		.then(function(resp){
			//表格
        	$('#datatable').DataTable({
        		data:resp.data.list,
        	  	 bDestroy : true,
        		columns: $this.columns,
        	});
        	//分页
        	laypage({
                cont: $('#pageNav'), // 容器
                pages: resp.data.totalPage,     // 总页数(后台的)
                skip: true, //是否开启跳页
			    skin: '#AF0000',
                curr: resp.data.curPage, //当前页(后台获取到的)
                jump: function(obj, first){ //触发分页后的回调(单击页码后)
                	params.curPage=obj.curr;
                    if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                    	$this.tablePage(params);  // (被单击的页码)
                    }
                }
            });
		});
		
		
		
		
		
		/*$.ajax({
	        url: this.pageUrl,//这个就是请求地址对应sAjaxSource
	        data : params, 
	        type: 'POST',
	        dataType: 'json',
	        async: false,
	        success: function (resp) {
	        	//表格
	        	$('#datatable').DataTable({
	        		data:resp.data.list,
	        	  	 bDestroy : true,
	        		columns: $this.columns,
	        	});
	        	//分页
	        	laypage({
	                cont: $('#pageNav'), // 容器
	                pages: resp.data.totalPage,     // 总页数(后台的)
	                skip: true, //是否开启跳页
				    skin: '#AF0000',
	                curr: resp.data.curPage, //当前页(后台获取到的)
	                jump: function(obj, first){ //触发分页后的回调(单击页码后)
	                	params.curPage=obj.curr;
	                    if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
	                    	$this.tablePage(params);  // (被单击的页码)
	                    }
	                }
	            });
	        	
	        }
	    });*/
	}
	
	
	/**
	 * 删除按钮
	 */
    this.delBtn = function(id){
		return '<a title="删除" href="javascript:;" onclick="table.del(this,\''+id+'\')" class="btn btn-danger radius size-MINI" style="margin-right:10px;" >删除</a>';
	}

    /**
     * 编辑按钮
     */
    this.editBtn = function(title,url,type){
		return '<a title="编辑" href="javascript:;" onclick="toEdit(\''+title+'\',\''+url+'\',\''+type+'\')" class="btn btn-success radius size-MINI" style="margin-right:10px;" >编辑</a>';
	}
    
    /**
     * 删除
     */
    this.del = function(obj,id){
    	if(seesionIsExist("admin") == "" || seesionIsExist("admin") == null) {
    		forwardLogin();
    		return false;
    	}
    	
    	
    	
    	var $this = this;
    	layer.confirm('确认要删除吗？',function(index){
    		//此处请求后台程序，下方是成功后的前台处理……
    		$.ajax({
    			type:"post",
    			url:$this.delUrl,
    			data:{"ids":id},
    			success:function(data){
    				if(data.code == "-3"){
    					forwardLogin();
    		    		return false;
    	        	}
    				if(data.code=="0"){
    					$(obj).parents("tr").remove();
    					layer.msg('删除成功',{icon:1,time:1000});
    				}else{
    					alert('error');
    				}
    			}
    		});
    		
    		
    	});
    }
    
    
    /**
     * 批量删除
     */
  //批量删除
    this.batchDel = function(){
    	if(seesionIsExist("admin") == "" || seesionIsExist("admin") == null) {
    		forwardLogin();
    		return false;
    	}
    	
    	
    	
    	var $this = this;
    	var ids = "";
    	var length = 0;
    	
    	//获取被选中的数据Id
    	$("tbody :checkbox").each(function(){
    		
    		if($(this).is(':checked')){
    			ids+=$(this).data("id")+",";
    			length++;
    		}
    		
    	});
    	
    	if(length >0){
    		
    		layer.confirm('确认要删除选中的'+length+'条数据吗？',function(index){
    			$.ajax({
    				type:"post",
    				url:$this.delUrl,
    				data:{"ids":ids},
    				async:false,
    				success:function(data){
    					if(data.code == "-3"){
    						forwardLogin();
    			    		return false;
    		        	}
    					if(data.code=="0"){
    						layer.msg('删除成功',{icon:1,time:1000});
    						$("table thead th input:checkbox").attr("checked",false);
    						$this.tablePage($this.params);
    					}else{
    						alert('error');
    					}
    				}
    			});
    		});
    		
    	}else{
    		layer.msg('请选择要删除的数据!',{icon:2,time:1000});
    		return false;
    	}
    }

} 







